package com.jacen.fzxs.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.jacen.fzxs.dao.ITopDao;
import com.jacen.fzxs.entity.TopEntity;
import com.jacen.fzxs.util.SqlCondition;

@Repository
public class TopDao extends BaseDao<TopEntity> implements ITopDao {

	@Override
	public TopEntity getTop() {
		String sql = "select * from t_web_top where status = 1";
		return executeForObject(sql);
	}

	@Override
	public void apply(String id) throws Exception {
		String sql = "update t_web_top set status = 1 where id = ?";
		executeForUpdate(sql,id);
		sql = "update t_web_top set status = 0 where id != ?";
		executeForUpdate(sql,id);
	}

	@Override
	public List<TopEntity> getPage(int beginRow, int endRow, Map<String, String> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "SELECT * from t_web_top  where 1=1 %s ";
		sql = String.format(sql, cond.getCondition());
		sql = SqlCondition.pageSql(sql, beginRow, endRow);
		return executeForList(sql, cond.getParams().toArray());
	}

	@Override
	public int getCount(Map<String, ? extends Object> params) {
		SqlCondition cond = getPageCondition(params);
		String sql = "select count(1) from t_web_top where 1=1 %s";
		sql = String.format(sql, cond.getCondition());
		return Long.valueOf(executeForLong(sql, cond.getParams().toArray())).intValue() ;
	}
	
	private SqlCondition getPageCondition(
			Map<String, ? extends Object> mapParams) {
		SqlCondition cond = new SqlCondition();
		return cond;
	}

}
